import 'package:cached_network_image/cached_network_image.dart';
import 'package:factoryside/about_us/about_me_page.dart';
import 'package:factoryside/about_us/feedback_page.dart';
import 'package:factoryside/api/api.dart';
import 'package:factoryside/bean/count_entity.dart';
import 'package:factoryside/bean/userinfo_entity.dart';
import 'package:factoryside/home/page_acc_list.dart';
import 'package:factoryside/home/page_setting.dart';
import 'package:factoryside/home/page_userinfo.dart';
import 'package:factoryside/home/page_wallet.dart';
import 'package:factoryside/provider/providers.dart';
import 'package:factoryside/utils/utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

///我的页面

class MinePage extends StatefulWidget {
  @override
  MinePageState createState() => new MinePageState();
}

class MinePageState extends State<MinePage> with AutomaticKeepAliveClientMixin {
  UserinfoDataData userInfo;
  String auth = '未认证';
  String sign = '电子合同';

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  void getData() {
    context.read<User>().getUserInfo(context);
  }
  Future unAuth() async {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            content: Text(
              '你还未实名认证，是否前往认证？',
              textAlign: TextAlign.center,
            ),
            actionsOverflowDirection: VerticalDirection.up,
            actions: [
              FlatButton(
                child: Text('暂不认证'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
              FlatButton(
                child: Text('前往认证'),
                onPressed: () async {
                  Navigator.of(context).pop();
//                  var result=await Navigator.push(this.context,
//                      CupertinoPageRoute(builder: (BuildContext context) {
//                        return VerifiedPage();
//                      }));
//                  if(result!=null){
//                    getUserInfo();
//                  }
                },
              ),
            ],
          );
        });
  }

  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  void _onRefresh() async {
    await Future.delayed(Duration(milliseconds: 1000));
    getData();
    _refreshController.refreshCompleted();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context); //必须实现 不然 在push过后页面会刷新

    return Scaffold(
      body: SmartRefresher(
          enablePullDown: true,
          enablePullUp: false,
          controller: _refreshController,
          onRefresh: _onRefresh,
          child: CustomScrollView(
            physics: BouncingScrollPhysics(),
            slivers: <Widget>[
              SliverList(
                delegate: SliverChildListDelegate([
//                  UserInfoHeader(userInfo: userInfo, countEntity: _countEntity),
                  Stack(
                    children: [
                      CachedNetworkImage(
                        imageUrl:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.sj520.cn%2Fsc%2Fima%2F20161225210724_99118.jpg&refer=http%3A%2F%2Fwww.sj520.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1624845365&t=55eaecb0f2882235780446c587e82bd8',
//                          'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2055879269,628258096&fm=26&gp=0.jpg',
//                          'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F779a72b90ef5123326aa89ce105c0ba23053196118d51-xLvuhO_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1624694130&t=8d6fc15379e81386a805a68da8251b5a',
                        fit: BoxFit.fill,
                        width: MediaQuery.of(context).size.width,
                        height: 120,
                      ),
                      Consumer<User>(builder: (context,user,child){
                        userInfo=user.userInfo;
                        if (userInfo!=null) {
                          if (userInfo.ifAuth == null) {
                                                    auth = '未认证';
                                                    unAuth();
                                                  } else if (userInfo.ifAuth == '1') {
                                                    auth = '已认证';
                                                  } else if (userInfo.ifAuth == '-1') {
                                                    auth = '认证未通过审核';
                                                  } else if (userInfo.ifAuth == '0') {
                                                    auth = '认证审核中';
                                                  }
                          return GestureDetector(
                            onTap: (){
                              push(context, UserInfo());
                            },
                            child: Container(
                              padding: EdgeInsets.fromLTRB(20, 50, 20, 20),
                              child: Column(
                                children: [
                                  Row(
                                    children: [
                                      Image.asset(
                                        'assets/avator.png',
                                        width: 50,
                                        height: 50,
                                      ),
                                      SizedBox(
                                        width: 20,
                                      ),
                                      Expanded(
                                        child: Column(
                                          crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                          children: [
                                            Row(
                                              children: [
                                                Expanded(
                                                    child: Text('${userInfo.companyName}',
                                                        style: TextStyle(
                                                            fontSize: 18,
                                                            color: Colors.white),
                                                        maxLines: 1,
                                                        overflow:
                                                        TextOverflow.ellipsis)),
                                                Image.asset(
                                                  'assets/right.png',
                                                  width: 5,
                                                  height: 5,
                                                ),
                                              ],
                                            ),
                                            Text(
                                              '${userInfo.userID.substring(0,3)}****${userInfo.phone.substring(userInfo.userID.length-4)}',
                                              style: TextStyle(
                                                  fontSize: 15, color: Colors.white),
                                            ),
                                          ],
                                        ),
                                      ),
//                                  SizedBox(width: 10,),
//                                  Image.asset('assets/avator.png',width: 20,height: 20,),
//                                  SizedBox(width: 10,),
//                                  Image.asset('assets/avator.png',width: 20,height: 20,),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          );
                        }else{
                          return Container();
                        }
                      }),
                    ],
                  ),
                  Column(
                    children: [
                      GestureDetector(
                        child: Container(
                          margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
                          color: Colors.white,
                          padding: EdgeInsets.symmetric(horizontal: 20),
                          height: 50,
                          child: Row(
                            children: [
                              Container(
                                  padding: EdgeInsets.only(right: 10),
                                  child: Image.asset(
                                    'assets/wallet.png',
                                    width: 20,
                                    height: 20,
                                  )),
                              Expanded(child: Text('我的钱包')),
                              Image.asset(
                                'assets/right.png',
                                width: 20,
                                height: 20,
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
//                          showToast('待开发...');
                    Navigator.push(this.context, CupertinoPageRoute(
                        builder: (BuildContext context) {
                          return WalletPage();
                        }));
                        },
                      ),
                      Divider(height: 1,),
                      Visibility(
                          visible: !Api.isSon,
                          child: Column(
                            children: [
                              GestureDetector(
                                child: Container(
                                  color: Colors.white,
                                  padding: EdgeInsets.symmetric(horizontal: 20),
                                  height: 50,
                                  child: Row(
                                    children: [
                                      Container(
                                          padding: EdgeInsets.only(right: 10),
                                          child: Image.asset(
                                            'assets/subaccount.png',
                                            width: 20,
                                            height: 20,
                                          )),
                                      Expanded(child: Text('我的产品')),
                                      Image.asset(
                                        'assets/right.png',
                                        width: 20,
                                        height: 20,
                                      ),
                                    ],
                                  ),
                                ),
                                onTap: () {
                                  showToast('待开发...');
                                  if (userInfo == null) {
                                    return;
                                  }
//                            Navigator.push(this.context, CupertinoPageRoute(
//                                builder: (BuildContext context) {
//                                  return SubManagerPage(false);
//                                }));
                                },
                              ),
                              Divider(
                                height: 1,
                              ),
                            ],
                          )),
                      Visibility(
                          visible: !Api.isSon,
                          child: Column(
                            children: [
                              GestureDetector(
                                child: Container(
                                  color: Colors.white,
                                  padding: EdgeInsets.symmetric(horizontal: 20),
                                  height: 50,
                                  child: Row(
                                    children: [
                                      Container(
                                          padding: EdgeInsets.only(right: 10),
                                          child: Image.asset(
                                            'assets/subaccount.png',
                                            width: 20,
                                            height: 20,
                                          )),
                                      Expanded(child: Text('子账号管理')),
                                      Image.asset(
                                        'assets/right.png',
                                        width: 20,
                                        height: 20,
                                      ),
                                    ],
                                  ),
                                ),
                                onTap: () {
                                  showToast('待开发...');
                                  if (userInfo == null) {
                                    return;
                                  }
//                            Navigator.push(this.context, CupertinoPageRoute(
//                                builder: (BuildContext context) {
//                                  return SubManagerPage(false);
//                                }));
                                },
                              ),
                              Divider(
                                height: 1,
                              ),
                            ],
                          )),
                      Visibility(
                          visible: !Api.isSon,
                          child: Column(
                            children: [
                              GestureDetector(
                                child: Container(
                                  color: Colors.white,
                                  padding: EdgeInsets.symmetric(horizontal: 20),
                                  height: 50,
                                  child: Row(
                                    children: [
                                      Container(
                                          padding: EdgeInsets.only(right: 10),
                                          child: Image.asset(
                                            'assets/subaccount.png',
                                            width: 20,
                                            height: 20,
                                          )),
                                      Expanded(child: Text('产品维修资料库')),
                                      Image.asset(
                                        'assets/right.png',
                                        width: 20,
                                        height: 20,
                                      ),
                                    ],
                                  ),
                                ),
                                onTap: () {
                                  showToast('待开发...');
                                  if (userInfo == null) {
                                    return;
                                  }
//                            Navigator.push(this.context, CupertinoPageRoute(
//                                builder: (BuildContext context) {
//                                  return SubManagerPage(false);
//                                }));
                                },
                              ),
                              Divider(
                                height: 1,
                              ),
                            ],
                          )),
                      SizedBox(height:
                        10,),
                      Column(
                        children: [
                          GestureDetector(
                            child: Container(
                              color: Colors.white,
                              padding: EdgeInsets.symmetric(horizontal: 20),
                              height: 50,
                              child: Row(
                                children: [
                                  Container(
                                      padding: EdgeInsets.only(right: 10),
                                      child: Image.asset(
                                        'assets/feedback.png',
                                        width: 20,
                                        height: 20,
                                      )),
                                  Expanded(child: Text('意见反馈')),
                                  Image.asset(
                                    'assets/right.png',
                                    width: 20,
                                    height: 20,
                                  ),
                                ],
                              ),
                            ),
                            onTap: () {
                              push(context, FeedbackPage());
                            },
                          ),
                          Divider(
                            height: 1,
                          ),
                        ],
                      ),
                      Column(
                        children: [
                          GestureDetector(
                            child: Container(
                              color: Colors.white,
                              padding: EdgeInsets.symmetric(horizontal: 20),
                              height: 50,
                              child: Row(
                                children: [
                                  Container(
                                      padding: EdgeInsets.only(right: 10),
                                      child: Image.asset(
                                        'assets/service.png',
                                        width: 20,
                                        height: 20,
                                      )),
                                  Expanded(child: Text('联系客服')),
                                  Image.asset(
                                    'assets/right.png',
                                    width: 20,
                                    height: 20,
                                  ),
                                ],
                              ),
                            ),
                            onTap: () {
                              FlutterPhoneDirectCaller.callNumber(
                                  '400-6262-365');
                            },
                          ),
                          Divider(
                            height: 1,
                          ),
                        ],
                      ),
                      Column(
                        children: [
                          GestureDetector(
                            child: Container(
                              color: Colors.white,
                              padding: EdgeInsets.symmetric(horizontal: 20),
                              height: 50,
                              child: Row(
                                children: [
                                  Container(
                                      padding: EdgeInsets.only(right: 10),
                                      child: Image.asset(
                                        'assets/about_us.png',
                                        width: 20,
                                        height: 20,
                                      )),
                                  Expanded(child: Text('关于我们')),
                                  Image.asset(
                                    'assets/right.png',
                                    width: 20,
                                    height: 20,
                                  ),
                                ],
                              ),
                            ),
                            onTap: () {
                              push(context, AboutMePage());
                            },
                          ),
                          Divider(
                            height: 1,
                          ),
                        ],
                      ),
                      GestureDetector(
                        child: Container(
                          color: Colors.white,
                          padding: EdgeInsets.symmetric(horizontal: 20),
                          margin: EdgeInsets.only(bottom: 50),
                          height: 50,
                          child: Row(
                            children: [
                              Container(
                                  padding: EdgeInsets.only(right: 10),
                                  child: Image.asset(
                                    'assets/setting.png',
                                    width: 20,
                                    height: 20,
                                  )),
                              Expanded(child: Text('设置')),
                              Image.asset(
                                'assets/right.png',
                                width: 20,
                                height: 20,
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          push(context, SettingPage());
                        },
                      ),
                    ],
                  )
                ]),
              ),
            ],
          )),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  void didUpdateWidget(MinePage oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }
}

class UserInfoHeader extends StatelessWidget {
  const UserInfoHeader({
    Key key,
    @required this.userInfo,
    @required CountEntity countEntity,
  }) : _countEntity = countEntity, super(key: key);

  final UserinfoDataData userInfo;
  final CountEntity _countEntity;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        CachedNetworkImage(
          imageUrl:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.sj520.cn%2Fsc%2Fima%2F20161225210724_99118.jpg&refer=http%3A%2F%2Fwww.sj520.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1624845365&t=55eaecb0f2882235780446c587e82bd8',
//                          'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2055879269,628258096&fm=26&gp=0.jpg',
//                          'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F779a72b90ef5123326aa89ce105c0ba23053196118d51-xLvuhO_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1624694130&t=8d6fc15379e81386a805a68da8251b5a',
          fit: BoxFit.fill,
          width: MediaQuery.of(context).size.width,
          height: 180,
        ),
        Container(
          padding: EdgeInsets.fromLTRB(20, 50, 20, 10),
          child: Column(
            children: [
              GestureDetector(
                onTap: (){
                  push(context, UserInfo());
                },
                child: Row(
                  children: [
                    Image.asset(
                      'assets/avator.png',
                      width: 50,
                      height: 50,
                    ),
                    SizedBox(
                      width: 20,
                    ),
                    Expanded(
                      child: Column(
                        crossAxisAlignment:
                        CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Expanded(
                                  child: Text('${userInfo.companyName}',
                                      style: TextStyle(
                                          fontSize: 18,
                                          color: Colors.white),
                                      maxLines: 1,
                                      overflow:
                                      TextOverflow.ellipsis)),
                              Image.asset(
                                'assets/right.png',
                                width: 5,
                                height: 5,
                              ),
                            ],
                          ),
                          Text(
                            '${userInfo.userID.substring(0,3)}****${userInfo.phone.substring(userInfo.userID.length-4)}',
                            style: TextStyle(
                                fontSize: 15, color: Colors.white),
                          ),
                        ],
                      ),
                    ),
//                                  SizedBox(width: 10,),
//                                  Image.asset('assets/avator.png',width: 20,height: 20,),
//                                  SizedBox(width: 10,),
//                                  Image.asset('assets/avator.png',width: 20,height: 20,),
                  ],
                ),
              ),
              SizedBox(
                height: 30,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: (){
                      push(context, AccListPage(0,initialIndex: 0));
                    },
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Text('${_countEntity.doCheckCount}',style: TextStyle(fontSize: 15,color: Colors.white),),
                        Text('配件待审核',style: TextStyle(fontSize: 15,color: Colors.white),),
                      ],
                    ),
                  ),
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: (){
                      push(context, AccListPage(1));
                    },
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Text('${_countEntity.remoteFeeDoCheckCount}',style: TextStyle(fontSize: 15,color: Colors.white),),
                        Text('费用待审核',style: TextStyle(fontSize: 15,color: Colors.white),),
                      ],
                    ),
                  ),
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: (){
                      push(context, AccListPage(0,initialIndex: 1));
                    },
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Text('${_countEntity.doDeliverGoodsCount}',style: TextStyle(fontSize: 15,color: Colors.white),),
                        Text('待发件',style: TextStyle(fontSize: 15,color: Colors.white),),
                      ],
                    ),
                  ),
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: (){
                      push(context, AccListPage(0,initialIndex: 3));
                    },
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Text('${_countEntity.doReturnConfigCount}',style: TextStyle(fontSize: 15,color: Colors.white),),
                        Text('返件待确认',style: TextStyle(fontSize: 15,color: Colors.white),),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }
}
